const network_utils = require('../../../../http/NetworkUtils.js');
const constants = require('../../../../common/Constants.js');
const umfUtils = require('../../../../utils/UmfUtils.js');
const UmfToast = require('../../../../utils/UmfToast.js');
import PageRouter from '../../../../utils/PageRouter';

var app = getApp()
let pageOptions = {};
let page = null;
Page({
    data: {
        currtab: 0,
        swipertab: [{name: '月账单', index: 0}, {name: '店铺信息', index: 1}],
        tradeListArray: [],
        hasMore: false
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        page = 0;
        pageOptions = options;
        wx.setNavigationBarTitle({
            title: (options.title || "")
        });
        let res = app.globalData.systemInfo;

        this.setData({
            deviceW: res.windowWidth,
            deviceH: res.deviceHeight,
            fromPage: options.fromPage
        });

        this._netWorkRequest();
    },

    /**
     * @Explain：选项卡点击切换
     */
    tabSwitch: function (e) {
        var pushUrl = e.currentTarget.dataset.current == 0 ? './monthDeals/monthDeals?title=' + pageOptions.title + '&storeId=' + pageOptions.storeId : './storeInfo/storeInfo?storeId=' + pageOptions.storeId;
        e.currentTarget.dataset.current == 0 && PageRouter.wxNavigateTo(this.data.fromPage, pushUrl, this.data.fromPage === "clerkTransOverview");
        e.currentTarget.dataset.current == 1 && PageRouter.wxNavigateTo(this.data.fromPage, pushUrl, true);
    },

    cellClick: function (e) {
        //点击cell
        var payData = this.data.tradeListArray[e.currentTarget.dataset.current];
        if (payData.storeSumNum != "0") {
            let optionsData = JSON.stringify({
                'payTime': payData.date,
                'storeId': pageOptions.storeId,
                'title': pageOptions.title,
                'posReportId': pageOptions.posReportId
            });
            PageRouter.wxNavigateTo(this.data.fromPage, './payType/payType?optionsData=' + optionsData, this.data.fromPage === "clerkTransOverview");
        }
    },


    //加载更多
    loadMore: function () {
        this._netWorkRequest();
    },

    _netWorkRequest: function () {
        let that = this;
        let params = {
            storeId: pageOptions.storeId,
            pageNo: page + 1 + '',
            pageSize: app.globalData.pageSize,
            posReportId: pageOptions.posReportId
        };

        network_utils.buildRequest(constants.Interfaces.FUNC_Merchant_MonthTrading, params, {
            doHttpSucess: function (data) {
                if (constants.RetCodes.SUCCESS === data.retCode) {
                    page++;
                    let transactionArray = that.data.tradeListArray;
                    let tradeList = data["tradeList"];
                    //数据格式化
                    for (var i = 0; i < tradeList.length; i++) {

                        let amount = umfUtils.fentoyuan(tradeList[i]["storeSumAmount"]);
                        tradeList[i]["storeSumAmount"] = amount;
                        transactionArray.push(tradeList[i]);
                    }

                    let hasMore = tradeList.length === parseInt(app.globalData.pageSize);
                    !hasMore && UmfToast.showToast("已经加载完所有数据");
                    that.setData({
                        hasMore: hasMore,
                        tradeListArray: transactionArray
                    });
                    return true;
                }
                return false;
            }
        }).send();
    }
})